package cn.gcBlock.service.impl;

import cn.gcBlock.service.BlockNodeService;
import cn.gcBlock.service.BlockNodeTransService;
import cn.gcBlock.utils.BlockEncryptUtils;
import com.alibaba.fastjson.JSON;
import cn.gcBlock.exception.BadRequestException;
import cn.gcBlock.event.SignSuccessEvent;
import cn.gcBlock.po.NodeEntity;
import cn.gcBlock.po.constant.NodeType;
import cn.gcBlock.repository.NodeRepository;
import cn.gcBlock.dto.NodeDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
public class BlockNodeTransServiceImpl implements BlockNodeTransService {
    @Autowired
    NodeRepository nodeRepository;
    @Autowired
    BlockNodeService blockNodeService;
    @Autowired
    ApplicationContext applicationContext;
    @Override
    public NodeDTO transBlock(NodeDTO resource) {
        NodeEntity nodeEntity = new NodeEntity();
        BeanUtils.copyProperties(resource, nodeEntity);
        String entity = JSON.toJSONString(nodeEntity);
        boolean boo = BlockEncryptUtils.verify(entity, resource.getSignature(), resource.getPublicKey());
        //验证签名
        if (!boo) {
            throw new BadRequestException("签名错误");
        }
        if (NodeType.CREATION.getCode().equals(nodeEntity.getType())) {
            //创生，创生的名字不能重复
            if(blockNodeService.isExistCreationBlock(nodeEntity.getSymbol())){
                throw new BadRequestException("无法创生已存在的区块链");
            }
            //锁定成功或初始化成功，或配置项成功
        }
        //初始化区块链
            NodeDTO nodeDTO = blockNodeService.findCreation(nodeEntity.getSymbol());
            if(nodeDTO==null&&!NodeType.CREATION.getCode().equals(nodeEntity.getType())){
                throw new BadRequestException("服务区块不存在此种类型的货币");
            }
        //开始转账操作，查询余额
        if (NodeType.COIN.getCode().equals(nodeEntity.getType())) {
            boolean isPublicServer = blockNodeService.isPublicServer(nodeEntity.getPublicKey());
            if(isPublicServer){
                //服务者是需要冻结的
                throw new BadRequestException("您已经操作冻结钱包，请解除冻结后再进行转账");
            }
            //查询对方账户是否存在
            if (!blockNodeService.isExistBlock(nodeEntity.getToPublicKey(), nodeEntity.getSymbol())) {
                throw new BadRequestException("对方账户不存在");
            }
            double lessMoney = blockNodeService.getRemainingSum(nodeEntity.getPublicKey());
            //支出的金额
            double cashOutMoney = nodeEntity.getCoin();
            if(cashOutMoney==0){
                throw new BadRequestException("转账金额大于0");
            }
            if (!(lessMoney - cashOutMoney > 0)) {
                throw new BadRequestException("余额不足");
            }
        }
        NodeDTO result = blockNodeService.create(resource);
        SignSuccessEvent signSuccessEvent = new SignSuccessEvent(applicationContext,resource);
        applicationContext.publishEvent(signSuccessEvent);
        return result;
    }

}
